package com.notary.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;

public class DesUtil {
    private static AlgorithmParameterSpec iv = null;// 加密算法的参数接口，IvParameterSpec是它的一个实现
    private static SecretKey key = null;


    public DesUtil(String access_token, String desKey, String desIv) throws Exception {
        byte[] DESkey = desKey.getBytes();
        byte[] DESIV = desIv.getBytes();
        DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
        iv = new IvParameterSpec(DESIV);// 设置向量
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
        key = keyFactory.generateSecret(keySpec);// 得到密钥对象
    }

    /**
     * 加密
     *
     * @param data 待加密的数据
     * @return 加密后的数据
     * @throws Exception
     */
    public static String encode(String desKey, String desIv, String data) throws Exception {
        byte[] DESkey = desKey.getBytes();
        byte[] DESIV = desIv.getBytes();
        DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
        iv = new IvParameterSpec(DESIV);// 设置向量
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
        key = keyFactory.generateSecret(keySpec);// 得到密钥对象
        Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
        enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式，给出密钥和向量
        byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
        return Base64.getEncoder().encodeToString(pasByte);
    }

    /**
     * 解密
     *
     * @param data 解密前的数据
     * @return 解密后的数据
     * @throws Exception
     */
    public static String decode(String desKey, String desIv, String data) throws GeneralSecurityException, IOException {
        byte[] DESkey = desKey.getBytes();
        byte[] DESIV = desIv.getBytes();
        DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
        iv = new IvParameterSpec(DESIV);// 设置向量
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
        key = keyFactory.generateSecret(keySpec);// 得到密钥对象
        Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        deCipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] pasByte = deCipher.doFinal(Base64.getDecoder().decode(data));
        return new String(pasByte, "UTF-8");
    }


    private static String deskey = "Ds6uzbgq2AUt2iMji/2n6g==";
    private static String desIv = "26153472";

    public static void main(String[] args) throws Exception {
        String data = "u5rfs6vu7DUBq15DH4UdG8pWQ2hH0NKXCkRc0Im97bi3peOOA01Ky87LGUvxdcJnqCzQIGxzpeS59JnbPy1tpOs/f+1HqzOAhrUuD0CH52wYVeZFNJ6kSsH87JTEyaDg4LXWTUDUpVWv1eTA6k+WPphuzuM4VMrk8TuXBtpPUMkbuUo6IasuOFu3170/bKOKBBBekooVS12KBMyFYYpm/QR9CwulBsrNCUZxISCUsdSQBgB5nOHgd2LPsXdjddqbJ2/PYd7jnJ8hDtjY0e1PR60WolKqsL4q7eprvQVvufy1rvTboC0MEcrkoBixMeKXaQhUfZBpVVkxluQb5uREVH+0/34LOODQp5E5wh2RoLRwcQr9WLnoo0DIHfnWQLb9W76gBx4Ba3PW2eJ7UTxbudPMkr2QUe2iQoi+JYnXCfnZP5O/uvWI+OmL74oKC0QZjrWIxQ9yQ6ARKgGLRzTgIw==";
        System.out.println(DesUtil.decode(deskey, desIv, data));

    }


}
